package com.ruoyi.project.module.cjl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.YqgBarBills;
import com.ruoyi.project.domain.YqgConsumptionDetails;
import com.ruoyi.project.domain.YqgHands;
import com.ruoyi.project.domain.YqgInformationRoom;
import com.ruoyi.project.module.ljw.LjwYqgQubingController;
import com.ruoyi.project.service.IYqgBarBillsService;
import com.ruoyi.project.service.IYqgConsumptionDetailsService;
import com.ruoyi.project.service.IYqgHandsService;
import com.ruoyi.project.service.IYqgInformationRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 消费详细Controller
 *
 * @author dr
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/cjl/details")
public class CjlYqgConsumptionDetailsController extends BaseController {
    @Autowired
    private IYqgConsumptionDetailsService yqgConsumptionDetailsService;
    @Autowired
    private IYqgBarBillsService yqgBarBillsService;
    @Autowired
    private IYqgHandsService yqgHandsService;
    @Autowired
    private IYqgInformationRoomService yqgInformationRoomService;

    /**
     * 查询消费详细列表
     */
    @RequiresPermissions("project:details:list")
    @GetMapping("/list")
    public TableDataInfo list(YqgConsumptionDetails yqgConsumptionDetails) {
        startPage();
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        return getDataTable(list);
    }

    /**
     * 导出消费详细列表
     */
    @RequiresPermissions("project:details:export")
    @Log(title = "消费详细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgConsumptionDetails yqgConsumptionDetails) {
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        ExcelUtil<YqgConsumptionDetails> util = new ExcelUtil<YqgConsumptionDetails>(YqgConsumptionDetails.class);
        util.exportExcel(response, list, "消费详细数据");
    }

    /**
     * 获取消费详细详细信息
     */
    @RequiresPermissions("project:details:query")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(yqgConsumptionDetailsService.selectYqgConsumptionDetailsByID(ID));
    }

    /**
     * 新增消费详细
     */
    @RequiresPermissions("project:details:add")
    @Log(title = "消费详细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqgConsumptionDetails yqgConsumptionDetails) {
        return toAjax(yqgConsumptionDetailsService.insertYqgConsumptionDetails(yqgConsumptionDetails));
    }

    /**
     * 修改消费详细
     */
    @RequiresPermissions("project:details:edit")
    @Log(title = "消费详细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqgConsumptionDetails yqgConsumptionDetails) {
        return toAjax(yqgConsumptionDetailsService.updateYqgConsumptionDetails(yqgConsumptionDetails));
    }

    /**
     * 删除消费详细
     */
    @RequiresPermissions("project:details:remove")
    @Log(title = "消费详细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(yqgConsumptionDetailsService.deleteYqgConsumptionDetailsByIDs(IDs));
    }

    /**
     * 留牌
     */
    /*@RequiresPermissions("project:details:edit")*/
    @Log(title = "留牌", businessType = BusinessType.LEAVECARD,logicalKey = "beforeLeave")
    @PutMapping("/liu")
    public AjaxResult edithand(@RequestBody YqgConsumptionDetails yqgConsumptionDetails) {
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionBrand(yqgConsumptionDetails.getHandBrand());//消费表留牌手牌
        List<YqgBarBills> list1 = yqgConsumptionDetailsService.selectYqgBarBillsBrand(yqgConsumptionDetails.getNote1());//吧台结账手牌
        List<YqgBarBills> list2 = yqgConsumptionDetailsService.selectYqgBarBillsBrand(yqgConsumptionDetails.getHandBrand());//吧台留牌手牌

        //设置结账手牌查询条件
        YqgHands yqgHands = new YqgHands();
        yqgHands.setHandBrand(yqgConsumptionDetails.getNote1());
        List<YqgHands> Handlist = yqgHandsService.selectYqgHandsList(yqgHands);//结账手牌
        for (YqgHands yqgHands1 : Handlist) {
            if (yqgHands1.getLockedState().equals("0")) {
                throw new ServiceException("结账手牌已锁定，请重新操作！");
            }
        }
        //设置留牌手牌查询条件
        YqgHands yqgHands2 = new YqgHands();
        yqgHands2.setHandBrand(yqgConsumptionDetails.getHandBrand());
        List<YqgHands> Handlist1 = yqgHandsService.selectYqgHandsList(yqgHands2);//结账手牌
        for (YqgHands yqgHands3 : Handlist1) {
            if (yqgHands3.getLockedState().equals("0")) {
                throw new ServiceException("留牌手牌已锁定，请重新操作！");
            }
        }
        /*if(list.isEmpty()){
            throw new ServiceException("留牌手牌没有消费记录，请重新操作！");
        }*/
        //判断吧台表里结账手牌是不是手牌状态都为空
        int n = 0;
        for (YqgBarBills yqgBarBills : list1) {
            if (yqgBarBills.getHandState().equals("1")) {
                n = n + 1;
            }
        }
        if (n == 0) {
            throw new ServiceException("结账手牌是空牌，请重新操作！");
        }
        //判断吧台表里留牌手牌是不是手牌状态都为空
        int m = 0;
        for (YqgBarBills yqgBarBills1 : list2) {
            if (yqgBarBills1.getHandState().equals("1")) {
                m = m + 1;
            }
        }
        if (m == 0) {
            throw new ServiceException("留牌手牌是空牌，请重新操作！");
        }
        for (YqgConsumptionDetails yqgConsumptionDetails1 : list) {
            if (yqgConsumptionDetails1.getNextClockStatus() != null && yqgConsumptionDetails1.getNextClockStatus().equals("1")) {
                throw new ServiceException("留牌手牌未下钟，请重新操作！");
            }
        }
        for (YqgConsumptionDetails yqgConsumptionDetails2 : list) {
            if (yqgConsumptionDetails2.getCheckOutStatus() != null && yqgConsumptionDetails2.getCheckOutStatus().equals("1")) {
                throw new ServiceException("留牌手牌未退房，请重新操作！");
            }
        }
        //判断消费表里留牌手牌是不是手牌状态都为空
        int q = 0;
        for (YqgConsumptionDetails yqgConsumptionDetails5 : list) {
            if (yqgConsumptionDetails5.getHandState().equals("1")) {
                q = q + 1;
            }
        }
        if (q == 0) {
            throw new ServiceException("留牌手牌是空牌，请重新操作！");
        }
        // 若并牌，则先取并
        for (YqgConsumptionDetails cd : list) {
            if (cd.getHandState().equals("1")) {
                if (cd.getMergeStatus().equals("0")) {
                    YqgConsumptionDetails details = new YqgConsumptionDetails();
                    details.setBeforeLeave(yqgConsumptionDetails.getHandBrand());
                    details.setMergeHands(cd.getMergeHands());
                    this.qubing(details);
                }
            }
        }
        // 重新获取list
        list = yqgConsumptionDetailsService.selectYqgConsumptionBrand(yqgConsumptionDetails.getHandBrand());
        // 然后再留牌
        for (YqgBarBills c : list1) {
            if (c.getHandState().equals("1")) {
                for (YqgBarBills b : list2) {
                    if (b.getHandState().equals("1")) {
                        for (YqgConsumptionDetails a : list) {
                            if (a.getHandState().equals("1")) {
                                a.setHandBrand(yqgConsumptionDetails.getNote1());
                                a.setBeforeLeave(yqgConsumptionDetails.getHandBrand());
                                a.setRetentionStatus("0");
                                Date d = new Date();
                                a.setLeaveTime(d);
                                a.setBeforeBarBillsId(b.getID());
                                a.setBarBillsId(c.getID());
                                a.setNote10(yqgConsumptionDetails.getNote1()); //作为留牌手牌使用，取消留牌通过note10查找
                                yqgConsumptionDetailsService.updateYqgConsumptionDetails(a);
                                YqgBarBills yqgBarBills = yqgBarBillsService.selectYqgBarBillsByID(b.getID());
                                yqgBarBills.setRetentionStatus("0");
                                yqgBarBillsService.updateYqgBarBills(yqgBarBills);
                            }
                        }
                    }
                }
            }

        }
        return success();
    }


    /**
     * 取并
     */
    public void qubing(YqgConsumptionDetails yqgConsumptionDetails) {
        String handId = yqgConsumptionDetails.getBeforeLeave();
        // 设参数
        // yqgConsumptionDetails.setMergeHands("");
        yqgConsumptionDetails.setHandState("1");
        yqgConsumptionDetails.setMergeStatus("0");
        // 修改消费详细表
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        if (list.size() == 0) {
            yqgConsumptionDetails.setHandBrand(yqgConsumptionDetails.getBeforeLeave());
            yqgConsumptionDetails.setBeforeLeave(null);
            List<YqgConsumptionDetails> yqgCDL = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
            if (yqgCDL.size() != 0) {
                // 批量修改
                for (YqgConsumptionDetails consumptionDetails : yqgCDL) {
                    //list.get(i).setDrawTime(drawtime);
                    consumptionDetails.setMergeHands("");
                    consumptionDetails.setMergeStatus("1");
                    consumptionDetails.setMergeBarBillsId(0L);
                    if (consumptionDetails.getBeforeLeave() != null) {
                        consumptionDetails.setRetentionStatus("0");
                    }
                    yqgConsumptionDetailsService.updateYqgConsumptionDetails(consumptionDetails);
                }
            }
        }
        //if (list.size() != 0) {
        else {
            // 批量修改
            for (YqgConsumptionDetails consumptionDetails : list) {
                //list.get(i).setDrawTime(drawtime);
                consumptionDetails.setMergeHands("");
                consumptionDetails.setMergeStatus("1");
                consumptionDetails.setMergeBarBillsId(0L);
            }
            // 批量更新
            for (YqgConsumptionDetails consumptionDetails : list) {
                yqgConsumptionDetailsService.updateYqgConsumptionDetails(consumptionDetails);
            }
        }

        // 手牌表
        YqgHands yqgHands = new YqgHands();
        yqgHands.setHandBrand(handId);
        List<YqgHands> handlist = yqgHandsService.selectYqgHandsList(yqgHands);
        if (handlist.size() != 0) {
            // 更新手牌表
            yqgHands = handlist.get(0);
            yqgHands.setHandState("1");
            yqgHands.setLockedState("1");
            yqgHandsService.updateYqgHands(yqgHands);
        }

        // 吧台账单表
        YqgBarBills yqgBarBills = new YqgBarBills();
        yqgBarBills.setHandBrand(handId);
        // 手牌状态为在台
        yqgBarBills.setHandState("1");
        List<YqgBarBills> billList = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);
        if (billList.size() != 0) {
            // 更新吧台账单表
            yqgBarBills = billList.get(0);
            yqgBarBills.setMergeStatus("1");
            yqgBarBillsService.updateYqgBarBills(yqgBarBills);
        }

        // 更新房间信息表
        YqgInformationRoom yqgInformationRoom = new YqgInformationRoom();
        yqgInformationRoom.setHandBrand(handId);
        // 手牌状态为在台
        yqgInformationRoom.setHandState("1");
        List<YqgInformationRoom> roomlist = yqgInformationRoomService.selectYqgInformationRoomList(yqgInformationRoom);
        if (roomlist.size() != 0) {
            // 更新房间信息表
            yqgInformationRoom = roomlist.get(0);
            yqgInformationRoom.setHandState("1");
            yqgInformationRoomService.updateYqgInformationRoom(yqgInformationRoom);
        }
    }


}
